using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using taxi_sovereign.model;
using taxi_sovereign.interfaces;


namespace taxi_sovereign.analyze
{
    public static class Analyzer
    {
        static Model currmodel = new Model();
        static Hashtable vehiclesPassn = new Hashtable();
        static Hashtable stopsMood = new Hashtable();
        static StringBuilder analyzeBuilder = new StringBuilder();
        static int ticks;
        public static void SetModel(IModel InModel)
        {
            currmodel = (Model)InModel;
            AnalyzerInit();
        }
        static void AnalyzerInit()
        {
            int i = 0;
            ticks = 0;
            foreach (IVehicle v in currmodel.GetModelSettings().Vehicles())
            {
                //float percnt= (float)v.PassengersCurrent/(float)_Constants._maxpassengers;
                vehiclesPassn.Add(v, i);
            }
            foreach (Road r in currmodel.GetModelSettings().Roads())
            {
                foreach (IStop s in r.GetStopArray((Cross)r.GetFirstCross()))
                {
                    stopsMood.Add(s, i);
                }
            }
        }
        public static void AnlBusCapacity()
        {
            foreach (IVehicle ve in currmodel.GetModelSettings().Vehicles()) 
            {
                //IVehicle ve = (IVehicle)veh;
                float curPasPercnt =(float)ve.PassengersCurrent/(float)_Constants._maxpassengers;
                if (!vehiclesPassn.Contains(ve))
                {
                    vehiclesPassn.Add(ve, 0);
                }
                if (curPasPercnt < 0.4F)
                {
                    vehiclesPassn[ve] = (int)vehiclesPassn[ve] + 1;
                    analyzeBuilder.AppendFormat("Vehicle \n{0} is too empty for {1}% of time!\n",ve,(int)(100*(float)((int)vehiclesPassn[ve])/(float)ticks));
                }
            }
        }

        public static void AnlStopMood()
        {
            foreach (Road r in currmodel.GetModelSettings().Roads())
            {
                foreach (IStop st in r.GetStopArray((Cross)r.GetFirstCross()))
                {
                    if (!stopsMood.Contains(st))
                    {
                        stopsMood.Add(st, 0);
                    }
                    if (st.AveragePassMood() < 0.4F)
                    {
                        stopsMood[st] = (int)stopsMood[st] + 1;
                        analyzeBuilder.AppendFormat("stop \n{0} : passengers are too sad for {1}% of time!\n", st, (int)(100 * (float)((int)stopsMood[st]) / (float)ticks));
                    }
                }
            }
        }

        public static string GetResults()
        {
            string re= analyzeBuilder.ToString();
            analyzeBuilder = new StringBuilder();
            return re;
        }
        public static void AnlTick()
        {
            ticks++;
        }

    }
}
